package com.shop.loan.model;

import com.shop.loan.model.entity.GoodsByMybatis;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.SQLDelete;
import xyz.erupt.annotation.Erupt;
import xyz.erupt.annotation.EruptField;
import xyz.erupt.annotation.sub_erupt.Filter;
import xyz.erupt.annotation.sub_erupt.Tree;
import xyz.erupt.annotation.sub_field.Edit;
import xyz.erupt.annotation.sub_field.EditType;
import xyz.erupt.annotation.sub_field.View;
import xyz.erupt.annotation.sub_field.ViewType;
import xyz.erupt.annotation.sub_field.sub_edit.AttachmentType;
import xyz.erupt.annotation.sub_field.sub_edit.BoolType;
import xyz.erupt.annotation.sub_field.sub_edit.ReferenceTreeType;
import xyz.erupt.annotation.sub_field.sub_edit.Search;
import xyz.erupt.upms.model.base.HyperModel;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

@Erupt(name = "商品类别",
        orderBy = "Classify.bySort desc",
        filter = @Filter("Classify.isDelete = false"),
        tree = @Tree(pid = "topClassify.id")
)
@Table(name = "s_classify")
@SQLDelete(sql = "update r_classify set is_delete = true, delete_time = now() where id = ?")
@Entity
@ApiModel("商品类别")
@Getter
@Setter
public class Classify extends HyperModel {

    @EruptField(
            views = @View(title = "名称"),
            edit = @Edit(title = "名称", notNull = true)
    )
    @ApiModelProperty(name = "name", value = "名称", dataType = "string", example = "1")
    private String name;

    @EruptField(
            views = @View(title = "图标", type = ViewType.IMAGE),
            edit = @Edit(title = "图标", type = EditType.ATTACHMENT,
                    attachmentType = @AttachmentType(type = AttachmentType.Type.IMAGE))
    )
    @ApiModelProperty(name = "logo", value = "图表", dataType = "string", example = "1")
    private String logo;

    @EruptField(
            views = @View(title = "排序数值"),
            edit = @Edit(title = "排序数值", desc = "数值大的在前", type = EditType.NUMBER)
    )
    private Integer bySort;

    @ManyToOne
    @JoinColumn(name = "top_classify_id")
    @EruptField(
            edit = @Edit(
                    title = "上级类目",
                    type = EditType.REFERENCE_TREE,
                    referenceTreeType = @ReferenceTreeType(pid = "topClassify.id", expandLevel = 0)
            )
    )
    private Classify topClassify;


    @EruptField(
            views = @View(title = "是否热门榜单"),
            edit = @Edit(
                    title = "是否热门榜单",
                    search = @Search,
                    type = EditType.BOOLEAN,
                    desc = "显示在首页的热门榜单",
                    notNull = true,
                    boolType = @BoolType(
                            trueText = "热门",
                            falseText = "非热门"
                    )
            )
    )
    @ApiModelProperty(name = "homeShow", value = "是否热门榜单", dataType = "int", example = "1")
    private Boolean homeShow = false;


    @ApiModelProperty(name = "topClassifyId", value = "上级类目Id", dataType = "int", example = "1")
    @Transient
    private Long topClassifyId;

    @ApiModelProperty(name = "goodsList", value = "商品类别下商品", dataType = "int", example = "1")
    @Transient
    private List<GoodsByMybatis> goodsList;

    private Boolean isDelete = false;

    private Date deleteTime;

    public Classify() {
    }

    public Classify(Long id) {
        setId(id);
    }
}
